package com.yzc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzc.execption.ZcException;
import com.yzc.mapper.UserMapper;
import com.yzc.model.entity.system.SysUser;
import com.yzc.model.query.system.UserInfoReq;
import com.yzc.model.vo.system.SysUserVo;
import com.yzc.model.vo.system.UserInfoVo;
import com.yzc.result.ResultCodeEnum;
import com.yzc.service.UserService;
import com.yzc.util.BCrypt;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser>
        implements UserService {

    @Autowired
    private BCrypt bCrypt;

    @Autowired
    private UserMapper userBeanMapper;

    public List<UserInfoVo> loadaAll() {
        //操作
        return userBeanMapper.loadaAll();
    }

    @Override
    public SysUserVo getUserVoByName(String username) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, username);
        SysUser sysUser = getOne(queryWrapper);

        if (sysUser == null) {
            return null;
        }

        SysUserVo sysUserVo = new SysUserVo();
        BeanUtils.copyProperties(sysUser, sysUserVo);
        return sysUserVo;
    }

    @Override
    public UserInfoVo getInfoById(Long id) {
        return userBeanMapper.getInfoById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultCodeEnum saveUser(UserInfoReq userInfoReq) {
        if (userInfoReq == null || StringUtils.isBlank(userInfoReq.getUsername())) {
            throw new ZcException(ResultCodeEnum.BAD_REQUEST);
        }
        try {

            String password = userInfoReq.getPassword();
            if (StringUtils.isNotBlank(password)) {
                userInfoReq.setPassword(bCrypt.encode(password));   //  有密码就加密密码
            }

            boolean isSaved = userBeanMapper.saveUser(userInfoReq);
            if (!isSaved) {
                return ResultCodeEnum.FAIL;
            }

            Long userId = userInfoReq.getId();

            Boolean isRoleSet = userBeanMapper.settingRole(userId, userInfoReq.getKzid());
            return isRoleSet ? ResultCodeEnum.SUCCESS : ResultCodeEnum.FAIL;

        } catch (Exception e) {
            throw new ZcException(ResultCodeEnum.FAIL.getCode(), "保存用户信息失败");
        }

    }

    @Override
    public ResultCodeEnum updateUser(UserInfoReq userInfoReq) {
        if (userInfoReq == null ||
                (userInfoReq.getId() == null && StringUtils.isBlank(userInfoReq.getUsername()))) {
            throw new ZcException(ResultCodeEnum.BAD_REQUEST.getCode(), "必须提供用户ID或用户名");
        }
        try {

            if (StringUtils.isNotBlank(userInfoReq.getPassword()))
                userInfoReq.setPassword(bCrypt.encode(userInfoReq.getPassword()));
            userBeanMapper.updateUser(userInfoReq);

        } catch (ZcException e) {
            throw new ZcException(ResultCodeEnum.BUSINESS_ERROR);
        } catch (Exception e) {
            throw new ZcException(ResultCodeEnum.FAIL.getCode(), "系统错误，更新用户信息失败");
        }

        return ResultCodeEnum.SUCCESS;
    }
}




